$(function () {
    var temp = location.search.substring(1);
    var arr = temp.split('&');
    // 再把arr里面的数据以 = 分割
    var params = {}
    arr.forEach(el => {
        var result = el.split('=') //result[0] --> key
        params[result[0]] = result[1];
    })
    var id = params.id;
    // http://localhost:3000/comment/playlist
    let currentComPage = 1;
    let limit = 20;
    let offset = (currentComPage - 1) * limit;
    // 最新评论

    let new_comments_wrap = $(`.new_comments_wrap`);
    $.ajax({
        url: 'http://localhost:3000/comment/playlist',
        data: {
            id: id,
            offset: offset
        },
        success: function (res) {
            sessionStorage.setItem('totalCom', res.total);
            new_comments_wrap.html(`<h3>最新评论(${res.total})</h3>`);
            res.comments.forEach(el => {
                let time = el.time;

                function getTime(time) {
                    let date = new Date(time);
                    let nowDate = new Date();
                    let nowYear = nowDate.getFullYear();
                    let year = date.getFullYear();
                    let month = date.getMonth();
                    let day = date.getDate();
                    date = date.getTime();
                    let dYear = nowYear - year;
                    let hour = Math.floor((date / 1000 / 60 / 60) % 24) + 8;
                    let min = Math.floor((date / 1000 / 60) % 60);

                    function format(val) {
                        return val < 10 ? '0' + val : val
                    };
                    hour = format(hour);
                    min = format(min);
                    if (dYear > 0) {
                        date = `${year}年${month}月${day}日 ${hour}:${min}`
                        return date
                    } else {
                        date = `${month}月${day}日 ${hour}:${min}`
                        return date
                    }
                };
                time = getTime(el.time);
                if (el.beReplied.length == 0) {
                    new_comments_wrap.append(`<div id="${el.commentId}${el.time}" class="itm" data-id="${el.commentId}">
                    <div class="head"><a href="javascript:;"><img
                                src="${el.user.avatarUrl}"></a>
                    </div>
                    <div class="cntwrap">
                        <div class="">
                            <div class="cnt f-brk"><a href="javascript:;"
                                    class="s-fc7">${el.user.nickname}</a>：${el.content}</div>
                        </div>
                        <div class="rp">
                            <div class="time s-fc4">${time}</div>
                            <span class="dlt">
                                <span style="display: none;" class="j-delete-comment">
                                    <a href="javascript:void(0)" class="s-fc3" data-id="15585584"
                                        data-type="delete">删除</a>
                                    <span class="sep">|</span>
                                </span></span>
                            <a data-id="15585584" class="likeNum" data-type="like"
                                href="javascript:void(0)">
                                <i class="zan u-icn2 u-icn2-12"></i> (${el.likedCount})</a>
                            <span class="sep">|</span>
                            <a href="javascript:void(0)" class="s-fc3" data-id="15585584"
                                data-type="reply">回复</a>
                        </div>
                    </div>
                </div>`)
                } else {
                    new_comments_wrap.append(`<div id="${el.commentId}${el.time}" class="itm" data-id="${el.commentId}">
                    <div class="head"><a href="javascript:;"><img
                                src="${el.user.avatarUrl}"></a>
                    </div>
                    <div class="cntwrap">
                        <div class="">
                            <div class="cnt f-brk"><a href="javascript:;"
                                    class="s-fc7">${el.user.nickname}</a>：${el.content}</div>
                        </div>
                            <div class="que f-brk f-pr s-fc3">
                                <span class="darr">
                                    <i class="bd">◆</i>
                                    <i class="bg">◆</i>
                                </span>
                                <a class="s-fc7" href="/user/home?id=47514954">${el.beReplied[0].user.nickname}</a>：${el.beReplied[0].content}。
                            </div>
                            <div class="rp">
                                <div class="time s-fc4">2015年4月18日</div>
                                <span class="dlt">
                                    <span style="display: none;" class="j-delete-comment">
                                        <a href="javascript:void(0)" class="s-fc3" data-id="15585584"
                                            data-type="delete">删除</a>
                                        <span class="sep">|</span>
                                    </span></span>
                                <a data-id="15585584" class="likeNum" data-type="like"
                                    href="javascript:void(0)">
                                    <i class="zan u-icn2 u-icn2-12"></i> (${el.likedCount})</a>
                                <span class="sep">|</span>
                                <a href="javascript:void(0)" class="s-fc3" data-id="15585584"
                                    data-type="reply">回复</a>
                            </div>
                        </div>
                    </div>`)
                }

            })

        }
    })

    // 精彩评论
    let good_comments_wrap = $('.good_comments_wrap')
    $.ajax({
        url: 'http://localhost:3000/comment/playlist',
        data: {
            id: id,
        },
        success: function (res) {
            console.log(res);
            sessionStorage.setItem('totalCom', res.total);
            good_comments_wrap.html('<h3>精彩评论</h3>');
            res.hotComments.forEach((el, index) => {
                let time = el.time;

                function getTime(time) {
                    let date = new Date(time);
                    let nowDate = new Date();
                    let nowYear = nowDate.getFullYear();
                    let year = date.getFullYear();
                    let month = date.getMonth();
                    let day = date.getDate();
                    date = date.getTime();
                    let dYear = nowYear - year;
                    let hour = Math.floor((date / 1000 / 60 / 60) % 24) + 8;
                    let min = Math.floor((date / 1000 / 60) % 60);

                    function format(val) {
                        return val < 10 ? '0' + val : val
                    };
                    hour = format(hour);
                    min = format(min);
                    if (dYear > 0) {
                        date = `${year}年${month}月${day}日 ${hour}:${min}`
                        return date
                    } else {
                        date = `${month}月${day}日 ${hour}:${min}`
                        return date
                    }
                };
                time = getTime(el.time);
                if (el.beReplied.length == 0) {
                    good_comments_wrap.append(`<div id="${el.commentId}${el.time}" class="itm" data-id="${el.commentId}">
                    <div class="head"><a href="javascript:;"><img
                                src="${el.user.avatarUrl}"></a>
                    </div>
                    <div class="cntwrap">
                        <div class="">
                            <div class="cnt f-brk"><a href="javascript:;"
                                    class="s-fc7">${el.user.nickname}</a>：${el.content}</div>
                        </div>
                        <div class="rp">
                            <div class="time s-fc4">${time}</div>
                            <span class="dlt">
                                <span style="display: none;" class="j-delete-comment">
                                    <a href="javascript:void(0)" class="s-fc3" data-id="15585584"
                                        data-type="delete">删除</a>
                                    <span class="sep">|</span>
                                </span></span>
                            <a data-id="15585584" class="likeNum" data-type="like"
                                href="javascript:void(0)">
                                <i class="zan u-icn2 u-icn2-12"></i> (${el.likedCount})</a>
                            <span class="sep">|</span>
                            <a href="javascript:void(0)" class="s-fc3" data-id="15585584"
                                data-type="reply">回复</a>
                        </div>
                    </div>
                </div>`);

                } else {
                    good_comments_wrap.append(`<div id="${el.commentId}${el.time}" class="itm" data-id="${el.commentId}">
                    <div class="head"><a href="javascript:;"><img
                                src="${el.user.avatarUrl}"></a>
                    </div>
                    <div class="cntwrap">
                        <div class="">
                            <div class="cnt f-brk"><a href="javascript:;"
                                    class="s-fc7">${el.user.nickname}</a>：${el.content}</div>
                        </div>
                            <div class="que f-brk f-pr s-fc3">
                                <span class="darr">
                                    <i class="bd">◆</i>
                                    <i class="bg">◆</i>
                                </span>
                                <a class="s-fc7" href="/user/home?id=47514954">${el.beReplied[0].user.nickname}</a>：${el.beReplied[0].content}。
                            </div>
                            <div class="rp">
                                <div class="time s-fc4">2015年4月18日</div>
                                <span class="dlt">
                                    <span style="display: none;" class="j-delete-comment">
                                        <a href="javascript:void(0)" class="s-fc3" data-id="15585584"
                                            data-type="delete">删除</a>
                                        <span class="sep">|</span>
                                    </span></span>
                                <a data-id="15585584" class="likeNum" data-type="like"
                                    href="javascript:void(0)">
                                    <i class="zan u-icn2 u-icn2-12"></i> (${el.likedCount})</a>
                                <span class="sep">|</span>
                                <a href="javascript:void(0)" class="s-fc3" data-id="15585584"
                                    data-type="reply">回复</a>
                            </div>
                        </div>
                    </div>`);

                }
            })
        }
    })

})